/*****************webSocket start**********************/
function webSocketReport(clientId, hospitalId) {
    if (!window.webSocketReportObj) {
        var webSocketUrl = '115.159.142.117/HospitalWebSocket/webSocketReport';
        /*var webSocketUrl = '192.168.1.73:8010/HospitalWebSocket/webSocketJoin';*/
        //判断当前浏览器是否支持WebSocket
        if ('WebSocket' in window) {
            if (location.protocol === "https:") {
                window.webSocketReportObj = new WebSocket("wss://" + webSocketUrl + "/" + clientId + "/" + hospitalId);
            } else {
                window.webSocketReportObj = new WebSocket("ws://" + webSocketUrl + "/" + clientId + "/" + hospitalId);
            }
        } else {
            alert('当前浏览器不支持 websocket');
        }
        //连接发生错误的回调方法
        webSocketReportObj.onerror = function () {
            console.log("<p>连接失败，请重新加载</p>");
        };

        //连接成功建立的回调方法
        webSocketReportObj.onopen = function () {
            console.log("WebSocket连接成功");
        };

        //接收到消息的回调方法
        window.messageObjArr = [];
        webSocketReportObj.onmessage = function (event) {
            var messageObj = JSON.parse(event.data);
            if (messageObj.cautionType === 5) {
                if (messageObj.processType === 1)
                    messageObjArr.push(messageObj);
            } else if (messageObj.processType !== 3) {
                messageObjArr.push(messageObj);
            }
            dealPushMessage();

            /* 处理推送消息闪烁 */
            alertList.prepend('<li class="blink" title="' + messageObj.content + '"><i></i><span>' + messageObj.content + '</span></li>');
            setTimeout(function () {
                alertList.find('li').eq(-1).remove();
                for(var i = 1; i < 8; i++) {
                    queryWarningList(i);
                }
            }, 30 * 1000);
        };

        //连接关闭的回调方法
        webSocketReportObj.onclose = function () {
            console.log("<p>您已处于离线状态</p>");
        };

        //监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
        window.onbeforeunload = function () {
            closeWebSocket();
        };

        window.dealPushMessage = function () {
            setTimeout(function () {
                $('.progress').html(messageObjArr.index + 1 + '/' + messageObjArr.length + '条');
                if ($('.score').is(':visible')) return;
                messageObjArr.index = (typeof messageObjArr.index === 'number' ? messageObjArr.index : -1) + 1;
                if (messageObjArr.index >= messageObjArr.length) return;
                var messageObj = messageObjArr[messageObjArr.index];
                $('.progress').html(messageObjArr.index + 1 + '/' + messageObjArr.length + '条');
                if (messageObj && (messageObj.cautionType === 1 || messageObj.cautionType === 2)) {
                    if (messageObj.processType === 1) {
                        $.eventAlert(messageObj);
                    } else if (messageObj.processType === 2){
                        $.eventAlert(messageObj);
                        // $.repairScore(messageObj.dispatchId, messageObj.repairId, messageObj.assetsId, messageObj.maintenanceType, 'report', true, messageObj);
                    }
                } else if (messageObj.cautionType === 5) {
                    if (messageObj.processType === 1) {
                        $.canshu(messageObj, true);
                    }
                }
            }, 1200);
        };

        //关闭WebSocket连接
        function closeWebSocket() {
            webSocketReportObj.close();
        }
    }
}
/*****************webSocket end**********************/